package com.csaba.db.ui;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

import com.csaba.db.DatabaseManager;
import com.csaba.util.LoggerHelper;

public class OpenDatabaseDialog extends Dialog
{
	private final static Logger logger = LoggerHelper.getDefaultLogger();

	private Combo database;
	private Text password;
	private final String selected;

	public OpenDatabaseDialog( final Shell parentShell, final String selected )
	{
		super(parentShell);
		this.selected = selected;
		setShellStyle(getShellStyle() | SWT.RESIZE);
	}

	@Override
	protected Control createDialogArea( final Composite parent )
	{
		final Composite composite = (Composite) super.createDialogArea(parent);

		final GridLayout layout = (GridLayout) composite.getLayout();
		layout.numColumns = 2;

		final Label label1 = new Label(composite, SWT.NONE);
		label1.setText("Select database:");
		database = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);

		if ( selected == null )
		{
			//fill combo with the project list
			final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
			final IProject[] projects = root.getProjects();
			for ( final IProject project : projects )
			{
				database.add(project.getName());
			}
			database.select(0);
		}
		else
		{
			database.add(selected);
			database.select(0);
			database.setEnabled(false);
		}

		database.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		final Label label2 = new Label(composite, SWT.NONE);
		label2.setText("Password:");
		password = new Text(composite, SWT.BORDER | SWT.SINGLE);
		password.setEchoChar('*');
		password.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		return composite;
	}

	@Override
	protected void configureShell( final Shell newShell )
	{
		super.configureShell(newShell);
		newShell.setText("Open Database");
	}

	@Override
	protected void okPressed()
	{
		try
		{
			DatabaseManager.getInstance().openDatabase(database.getItem(database.getSelectionIndex()),
					password.getText(), null);
		}
		catch ( final CoreException ex )
		{
			MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Error",
					"Error occured: " + ex.getMessage());
			logger.log(Level.SEVERE, "Error closing open database.", ex);
		}
		super.okPressed();
	}
}
